<?php
/**
*Authentication Library 
* */
class Auth{
	var $app;
	const CHKP_ADMIN_CODE = 'ADM';
	const CHKP_USER_CODE = 'USR';
	
	/**
	*Class constructor
	* */
	public function Auth(){
		$this->app = &get_instance(); 
		$this->app->load->model("m_users","action");
		log_message("debug","Auth class initialized via : ".get_class($this->app));
	}
	
	
	/**
	*Authenticates the user into the database
	* @param	$login the user's login
	* @param 	$pass user password 
	* */
	public function authenticate($login,$pass){
		log_message("debug","Authenticating user ".$login);
		if($this->app->action->mCheckUser($login,md5($pass))){
				$data = $this->app->action->mCheckUserDetails($login,md5($pass));
				if(!empty($data)){
					$this->app->session->set_userdata("us_",$data);				
					log_message("debug","User ".$login." authentified and session started");
					return true;
				}	
		}else{
			return false;
		}
	}
	
	/**
	* This method check to see if a connected user is an administrator or not
	* */
	public function isAdmin(){
		return $this->isUserType(self::CHKP_ADMIN_CODE);
	}
	
	/**
	 * Check to see if the this is a plain user
	 * **/
	public function isUser(){
		return $this->isUserType(self::CHKP_USER_CODE);
	}
	
	/**
	* Checks to see if the user is of a certain type
	* @param $sType user type to check
	* */
	public function isUserType($sType){
		$session = $this->app->session->userdata("us_");
		if($session != false){
			if($session["us_L"] != $sType){
				return false;
			}			
		}else{
			return false;
		}
		return true;
	}	
}
?>